/**
* Copyright @ 2023 iAuto (Shanghai) Co., Ltd.
* All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are NOT permitted except as agreed by
* iAuto (Shanghai) Co., Ltd.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*/


import VM from '@scene/scene-vm';
import {
  SVGRenderer as V2SVGAdapter,
  BitmapAdapter as V2BitmapAdapter,
} from 'scratch-svg-renderer';
import { Reducer } from 'redux';

import storage from '@/lib/storage';
import vmRuntime from '@/lib/vm-runtime';

export interface VmModelType {
  namespace: 'vm';
  state: any;
  reducers: {
    init: Reducer<any>;
    clear: Reducer<any>;
  };
  [extraProps: string]: any;
}

const VmModel: VmModelType = {
  namespace: 'vm',
  state: null,
  reducers: {
    init() {
      console.log('init vm ');
      const vm = new VM();
      vm.attachStorage(storage);
      storage.addOfficialScratchWebStores();

      // To be compatible with scratch2, so the svg and bitmap of scratch2 are available for scratch3
      vm.attachV2SVGAdapter(new V2SVGAdapter());
      vm.attachV2BitmapAdapter(new V2BitmapAdapter());

      vm.setCompatibilityMode(true);

      vmRuntime(vm.runtime);

      vm.start();
      return vm;
    },
    clear(state){
      console.log('clear vm ');
      state.clear();
      return null
    }
  },
};

export default VmModel;
